<HTML><HEAD>
<title>SASL Plugin Programmer's Guide</title>
<!-- $Id: plugprog.html,v 1.6 2011/09/07 12:41:51 mel Exp $ -->
</HEAD>
<BODY>
<h1>SASL Plugin Programmer's Guide</h1>

<h3>NOTE: This is a work in progress. Any contributions would be
<i>very</i> appreciated</h3>

<ul><h2>Contents</h2>
  <li><a href="#intro">Introduction</a></li>
  <ul>
    <li><a href="#about_this_guide">About this Guide</A></li>
    <li><a href="#about_sasl">What is SASL</A></li>
  </ul>
  <li><a href="#common">Common Section</a></li>
  <ul>
    <li><a href="#overview">Overview of Plugin Programming</a></li>
    <li><a href="#sasl_utils_t">Use of sasl_utils_t</a></li>
    <li><a href="#error_reporting">Error Reporting</a></li>
    <li><a href="#memory">Memory Allocation</a></li>
    <li><a href="#cslssl">Client Send First / Server Send Last</a></li>
  </ul>
  <li><a href="#client">Client Plugins</a></li>
  <li><a href="#server">Server Plugins</a></li>
  <li><a href="#canon_user">User Canonicalization (canon_user) Plugins</a></li>
  <li><a href="#auxprop">Auxiliary Property (auxprop) Plugins</a></li>
</ul>

<a name="intro"><h2>Introduction</h2></a>

    <a name="about_this_guide"><h3>About this Guide</h3></a>

    <p>This guide gives a <i>very</i> brief overview on the things that one
       needs to know to write a mechanism for the SASLv2 API (and thus
       Cyrus SASLv2).  Note that this page is a brief overview <i>only</i>
       and that the authoritative documentation are the header files
       included in the SASL distribution.  If you have any questions, please
       feel free to contact the Cyrus development team at
       <a href="mailto:cyrus-bugs@andrew.cmu.edu"><i>cyrus-bugs@andrew.cmu.edu
	</i></a> or the cyrus-sasl mailing list at
       <a href="mailto:cyrus-sasl@andrew.cmu.edu"><i>cyrus-sasl@andrew.cmu.edu
	</i></a>.</p>

    <p>Please note that this guide is only intended for developers looking
       to write mechanisms for the SASLv2 API, and that application programmers
       should be reading <a href="programming.html">this document</a> instead.
    </p>
   
    <a name="about_sasl"><h3>What is SASL?</h3></a>
    <p>A description of SASL is covered in detail in the
      <a href="programming.html">programmer's guide</a>, which mechanism
      developers should probably read first anyway to become familiar
      with development using the SASL library.
    </p>
    
<a name="common"><h2>Common Section</h2></a>
    <a name="overview"><h3>Overview of Plugin Programming</h3></a>

    <p>The basic idea behind programming plugins for Cyrus SASL rests in
       the ability to dlopen a shared library.  Thus, all plugins should
       be shared libraries.  It is recommended that they are libtool
       libraries for portability reasons (Cyrus SASL parses .la files to
       get the appropriate name to dlopen), but they can have an extention
       of .so as well.</p>
    <p>All plugins should live in the same directory
       (generally /usr/lib/sasl2), which the glue code (that is, the interface
       layer that sits between the plugins and the application) scans
       when one of the init functions (sasl_server_init or sasl_client_init)
       is called.  Cyrus SASL then attempts to open each library and
       run an initialization function.  If the initialization function
       succeeds, and the versions match, then the glue code determines
       that the load was successful and the plugin is available for use.</p>
    <p>There are serveral types of plugins (note that a given plugin library
       may contain any or all of the following in combination, though
       such a plugin would be a beast!):</p>
    <ul>
      <li><b>Mechanism Plugins</b> - These plugins implement mechanisms
	for authentication, and are the majority of the plugins included
	with Cyrus SASL.  Generally implementing both a client and a server
	side they take care of the authentication process.</li>
      <li><b>User Canonicalization Plugins</b> - These plugins enable differing
	ways of canonicalizing authentication and authorization IDs.</li>
      <li><b>Auxiliary Property Plugins</b> - These plugins allow auxilliary
	properties about user accounts to be looked up, such as passwords.
	Cyrus SASL includes a plugin to read sasldb files, for example.</li>
    </ul>
      
    <a name="sasl_utils_t"><h3>Use of sasl_utils_t</h3></a>

    <p>Because of the way that shared library plugins are loaded for both
       speed and namespace reasons, the symbol tables are not shared across
       plugins.  Thus, the only interface that the plugin should assume it
       has to the outside world is through the <tt>sasl_utils_t</tt> structure (or
       through links that it specifically requires).  Likewise, the glue code
       has no (and will use no) interface into the plugin other than the
       contents of the structures that are passed back to it by the
       initialization function.</p>
    <p>This should be stressed again: do not assume you have access to any
       functions except through links that your library explicitly makes
       or through what is provided via the <tt>sasl_utils_t</tt> structure.</p>

    <a name="error_reporting"><h3>Error Reporting</h3></a>
    <p>Error reporting is very important for failed authentication tracking
       and helping to debug installations or authentication problems.  For
       that reason, in addition to the standard SASL return codes, the
       glue code provides an interface to its seterror function (via
       <tt>sasl_utils_t</tt>).  This function sets detailed error information for
       a given connection.</p>
    <p><i>In order to ensure consistency of this information, it is the
	responsibility of the deepest function with access to the sasl_conn_t
	make the call to set the errdetail string.</i></p>

    <a name="memory"><h3>Memory Allocation</h3></a>
    <p>Memory allocation in SASLv2 follows the simple paradigm that if you
      allocate it, you free it.  This improves portability, and allows
      for a large performance improvement over SASLv1.  To prevent memory
      leaks (especially in the mechanism plugins), please ensure that you
      follow this paradigm.</p>

    <a name="cslssl"><h3>Client Send First / Server Send Last</h3></a>
    <p>Mechanism plugins used to have to worry about the situation
      where they needed clients to send first (or server to send last), yet
      the protocol did not support it.  Luckily, this is now handled by
      the glue code, provided that the plugin declares the appropriate flags
      in the structure returned by its init function.  Thus, the step functions
      will not have to worry about these issues and can be implemented
      knowing they will be called only when the application actually has
      data for them and/or will allow them to send data.  These flags are as
      follows:</p>
    <ul>
      <li><B>SASL_FEAT_WANT_CLIENT_FIRST</B>: The mechanism has the client
	side send first always.  (e.g. PLAIN)</li>
      <li><B>SASL_FEAT_SERVER_FIRST</B>: The mechanism has the server side
	send first always.  (e.g. CRAM-MD5)</li>
    </ul>

<p>If neither flag is set, the mechanism will handle the client-send
first situation internally, because the client may or may not send
first.  In this case, the plugin must
intelligently check for the presence (or absence) of clientin/serverin
data.  Note that the optional client send-first is only possible when the
protocol permits an initial response.

<p>The server send last situation is handled by the plugin intelligently
setting *serverout when the step function returns SASL_OK.  For mechanisms
which never send last (e.g. PLAIN), *serverout must be set to NULL.  For
mechanisms which always send last, *serverout must
point to the success data.  For mechanisms in which the server may or
may not send last (e.g. SRP), *serverout must be set accordingly.

<a name="client"><h2>Client Plugins</h2></a>
    <p>Client-side mechanism plugins are generally included in the same
       plugin with their <a href="#server">server</a> counterpart, though
       this is not a requirement.  They take care of the client-side of the
       SASL negotiation.  For a simple example, see the ANONYMOUS plugin.</p>
    <p>Client plugins must export <tt>sasl_client_plug_init</tt> which returns
       a <tt>sasl_client_plug_t</tt> in order to load.  The structure has
       several functional members and a global context (which applies to
       all connections using the plugin).  The important ones are described
       briefly here.</p>
    <ul>
      <li><b>mech_new</b> - Called at the beginning of each connection,
	(on a call to sasl_client_start),
	mech_new does mechanism-specific initialization, and if necessary
	allocates a connection context (which the glue code keeps track
	of for it).  mech_new does not actually send any data to the client,
        it simply allocates the context.</li>
      <li><b>mech_step</b> - Called from <tt>sasl_client_start</tt> and
	<tt>sasl_client_step</tt>, this function does the actual work of
	the client
	side of the authentication.  If authentication is successful, it
	should return SASL_OK, otherwise it should return a valid SASL
	error code (and call seterror).  This should also set up the
        oparams structure before returning SASL_OK, including any
        security layer information (in the way of callbacks).  Note
        that as soon as the client has both the authentication and
        authorization IDs, it MUST call the canon_user function provided
        in its params structure (for both the authentication and
	authorization IDs, with SASL_CU_AUTHID and SASL_CU_AUTHZID
        respectively).</li>
      <li><b>mech_dispose</b> - Called to dispose of a connection context.
	This is only called when the connection will no longer be used
        (e.g. when <tt>sasl_dispose</tt> is called)</li>
      <li><b>mech_free</b> - Called when the sasl library is shutting down
	(by <tt>sasl_client_done/sasl_server_done/sasl_done</tt>).
	Intended to free any global state of the plugin.</li>
    </ul>
<a name="server"><h2>Server Plugins</h2></a>
    <p>Server-side mechanism plugins are generally included in the same
       plugin with their <a href="#client">client</a> counterpart, though
       this is not a requirement.  They take care of the server-side of the
       SASL negotiation, and are generally more complicated than their
       client-side counterparts.  For a simple example, see the ANONYMOUS
       plugin.</p>
    <p>Server plugins must export <tt>sasl_server_plug_init</tt> which returns
       a <tt>sasl_server_plug_t</tt> in order to load.  The structure has
       several functional members and a global context (which applies to
       all connections using the plugin).  The important ones are described
       briefly here.</p>
    <ul>
      <li><b>mech_new</b> - Called at the beginning of each connection,
	(on a call to sasl_client_start),
	mech_new does mechanism-specific initialization, and if necessary
	allocates a connection context (which the glue code keeps track
	of for it).  mech_new does not actually send any data to the client,
        it simply allocates the context.</li>
      <li><b>mech_step</b> - Called from <tt>sasl_server_start</tt> and
	<tt>sasl_server_step</tt>, this function does the actual work of
	the server
	side of the authentication.  If authentication is successful, it
	should return SASL_OK, otherwise it should return a valid SASL
	error code (and call seterror).  This should also set up the
        oparams structure before returning SASL_OK, including any
        security layer information (in the way of callbacks and SSF
	information).  Also, as soon
        as the mechanism has computed both the authentication and the
        authorization IDs, it MUST call the canon_user function provided
        in the server params structure (for both the authentication and
	authorization IDs, with SASL_CU_AUTHID and SASL_CU_AUTHZID
        respectively).  This action will also fill in its
        propctx, so any auxiliary property <i>requests</i>
        (for example, to lookup
        the password) should be done before the request to canonicalize
        the authentication id.  Authorization ID lookups do not occur until
        after the plugin returns success to the SASL library.<p>

        Before returning SASL_OK, <tt>mech_step</tt> must fill in the
        oparams fields for which it is responsible, that is, <tt>doneflag</tt>
        (set to 1 to indicate a complete exchange), <tt>maxoutbuf</tt>, or
        the maximum output size it can do at once for a security layer,
        <tt>mech_ssf</tt> or the supplied SSF of the security layer,
        and <tt>encode</tt>, <tt>decode</tt>, <tt>encode_context</tt>,
	and <tt>decode_context</tt>,
        which are what the glue code will call on calls to <tt>sasl_encode</tt>,
	<tt>sasl_encodev</tt>, and <tt>sasl_decode</tt>.</li>
      <li><b>mech_dispose</b> - Called to dispose of a connection context.
	This is only called when the connection will no longer be used
        (e.g. when <tt>sasl_dispose</tt> is called)</li>
      <li><b>mech_free</b> - Called when the sasl library is shutting down
	(by <tt>sasl_client_done/sasl_server_done/sasl_done</tt>).
	Intended to free any global state of the plugin.</li>
      <li><b>setpass</b> - Called to set a user's password.  This allows
	mechanisms to support their own internal password or secret
	database.</li>
      <li><b>mech_avail</b> - Called by the first call to
	<tt>sasl_listmech</tt>,
	it checks to see if the mechanism is available for the given
	user, and MAY allocate a connection context (thus avoiding
	a call to <tt>mech_new</tt>).  However it should not do this
	without significant performance benefit as it forces the glue
	code to keep track of extra contexts that may not be used.</li>
    </ul>
<a name="canon_user"><h2>User Canonicalization (canon_user) Plugins</h2></a>
    <p>User Canonicalization plugins allow for nonstandard ways of
       canonicalizing the username.  They are subject to the following
       requirements:</p>
    <ul>
      <li>They must copy their output into the provided output buffers.</li>
      <li>The output buffers may be the same as the input buffers.</li>
      <li>They must function for the case which is only an authentication
	  ID (flags == SASL_CU_AUTHID) or only an authorization ID
	  (flags == SASL_CU_AUTHZID) or both
	  (flags == SASL_CU_AUTHID | SASL_CU_AUTHZID)
    </ul>
    <p>User canonicalization plugins must export a <tt>sasl_canonuser_init</tt>
       function which returns a <tt>sasl_canonuser_plug_t</tt> in order
       to load successfully.  They must implement at least one of
       the <tt>canon_user_client</tt> or <tt>canon_user_server</tt> members
       of the <tt>sasl_canonuser_plug_t</tt>.  The INTERNAL canon_user plugin
       that is inside of the glue code implements both in the same way.</p>
<a name="auxprop"><h2>Auxiliary Property (auxprop) Plugins</h2></a>
    <p>Perhaps the most exciting addition in SASLv2, Auxprop plugins
       allow for an easy way to perform password and secret lookups (as well
       as other information needed for authentication and authorization)
       from directory services, and in the same request allow the application
       to receive properties that it needs to provide the service.
    </p>
    <p>Auxprop plugins need to export the <tt>sasl_auxprop_init</tt> function
       and pass back a <tt>sasl_auxprop_plug_t</tt> in order to load
       successfully.  The sasldb plugin included with the Cyrus SASL
       distribution would be a good place to start.</p>
    <p>Interfacing with property contexts is extremely well documented in
       <tt>prop.h</tt> and so that is omitted here.  The only important
       note is to be sure that you are using the interfaces provided
       through the <tt>sasl_utils_t</tt> structure and not calling
       the functions directly.</p>
    <p>To successfully implement an auxprop plugin there is only one
       required function to implement, that is the <tt>auxprop_lookup</tt>
       member of the <tt>sasl_auxprop_plug_t</tt>.  This is called
       just after canonicalization of the username, with the canonicalized
       username.  It can then do whatever lookups are necessary for any
       of the requested auxiliary properties.</p>
</p>
<hr>
Back to the <A href=index.html>index</a>

</body>
</html>
